package org.geektimes.projects.user.service.impl;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.projects.user.validator.DelegatingValidator;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.validation.ConstraintViolation;
import java.util.Set;

/**
 * @author lanxing
 */
public class UserServiceImpl implements UserService {

    @Resource(name = "bean/DelegateEntityManager")
    private EntityManager entityManager;

    @Resource(name = "bean/Validator")
    private DelegatingValidator validator;

    private final static String RESULT_SUCCESS_MESSAGE = "registerSuccess";

    @Override
    public String register(User user) {
        String result =  RESULT_SUCCESS_MESSAGE;
        //校验user对象，返回第一个不符合规范的数据
        Set<ConstraintViolation<User>> violations = validator.validate(user);
        if(violations.iterator().hasNext()){
            return violations.iterator().next().getMessage();
        }
        entityManager.persist(user);
        return result;
    }

    @Override
    public boolean deregister(User user) {
        return false;
    }

    @Override
    public boolean update(User user) {
        return false;
    }

    @Override
    public User queryUserById(Long id) {
        return null;
    }

    @Override
    public User queryUserByNameAndPassword(String name, String password) {
        return null;
    }

}
